#!/bin/bash

if [ -n "$1" ] ;then
do_date=$1
else
    do_date=`date -v-1d +%Y-%m-%d`
fi
sql="
insert overwrite table test.userhis
select  userid, mobile, regdate, dt as start_date, '9999-12-31' as
end_date
  from test.userinfo
 where dt='$do_date'
union all
select B.userid,
       B.mobile,
       B.regdate,
       B.start_Date,
       case when B.end_date='9999-12-31' and A.userid is not null
            then date_add('$do_date', -1)
            else B.end_date
      end as end_date
  from (select * from test.userinfo where dt='$do_date') A
       right join test.userhis B
       on A.userid=B.userid;
"
hive -e "$sql"
